/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nxtgeninventorysystem;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import javax.swing.JTextArea;

/**
 *
 * @author MSI
 */
public class TextAreaAndFileOutputStream extends OutputStream {

    public static final String logfilename = "logs\\state-" + Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + "-" + Calendar.getInstance().get(Calendar.MONTH) + "-" + Calendar.getInstance().get(Calendar.YEAR) + ".log";
    private JTextArea target;
    private FileOutputStream writer;

    public TextAreaAndFileOutputStream(JTextArea t) {
        super();
        target = t;
        try {
            new File("logs").mkdir();
            writer = new FileOutputStream(logfilename, true);
        }
        catch (FileNotFoundException ex) {

        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void write(int b) {
        target.append("" + (char)(b));
        try {
            writer.write(b);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void setTextArea(JTextArea t) {
        target = t;
    }

    public JTextArea getTextArea() {
        return target;
    }

}
